<?php

namespace App\Http\Controllers\Web;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\ProductCategoriesRepository;
use Illuminate\Http\Request;
 
class CategoryController extends BaseController
{
   
    protected $category_repo;
    
    public function __construct(ProductCategoriesRepository $category_repo)
    {
        $this->category_repo = $category_repo;
    }

    public function index(Request $request)
    {
        $type = $request->input("type","");
        
        if($type == "all"){
            //为了和分页查询保持统一 这里处理成$ret['list'];
            $cates = $this->category_repo->orderBy("root_id","asc")->orderBy("pid","asc")->orderBy("sort","asc")->all();
            if(isset($cates['data'])){
                $ret['list'] = $cates['data'];
            }else{
                $ret['list'] = [];
            }
            return $this->success(ErrorConst::SUCCESS_CODE, $ret,false);
        }else{
            $cates = $this->category_repo->paginate($this->perPage);
          
            return $this->success(ErrorConst::SUCCESS_CODE, $cates,true);
        }
         
    }

    public function show($id)
    {
        $cate = $this->category_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $cate);
    }

    public function store(Request $request)
    {
        
    }

    public function update(Request $request, $id)
    { 
        
    }

    public function delete($id)
    {
        
    }
    
}
